package kr.co.bettersoft.sendme.rest;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import kr.co.bettersoft.sendme.provider.SendMeBlock;
import kr.co.bettersoft.sendme.provider.SendMeInstall;
import kr.co.bettersoft.sendme.provider.SendMeMember;
import kr.co.bettersoft.sendme.provider.SendMeRecognition;
import kr.co.bettersoft.sendme.util.SendMeCommon;
import kr.co.bettersoft.sendme.util.SendMeConstant;
import android.util.Log;
/**
 *  음성 메시지 인식 여부 관련 컨트롤러
 *   updateReadYn : 음성 인식 업데이트(읽음 처리) 
 * @author blue
 *
 */
public class SendMeRecognitionRest {					

	public static String TAG = SendMeRecognitionRest.class.getSimpleName();
	
	
//	sendMeRecognitionController

//	updateReadYn	음성 메시지 인식여부를 업데이트 한다.		SendMeRecognition.Result	
//	phoneNumber	
//	readYn	
//	activateYn	
//	activateYn	
//	modifyDate	
//	sendMeVoiceIdSendMeVoice	
//	sendMeVoiceSender	

	
	
//	SendMeRecognition	
	
//	phoneNumber	사용자 휴대전화 번호.(PK)
//	readYn	음성 메시지 읽음 여부.(Y/N)
//	activateYn	활성화 여부.
//	modifyDate	수정 일자.
//	registerDate	등록 일자.
//	sendMeVoiceIdSendMeVoice	음성 메시지 테이블 아이디.(FK)
//	sendMeVoiceSender	음성 메시지 생성자.(FK)
//	totalCount	총 갯수.
//	result	결과.

	/**
	 * 음성 메시지 읽음 여부를 업데이트한다. (읽음 처리)
	 * 
	 * @param sendMeRecognition
	 * @return
	 */
	public SendMeRecognition updateReadYn(SendMeRecognition sendMeRecognition) {
		Log.i(TAG, "updateReadYn");
		
		String url = SendMeConstant.SERVER_URL
			+ SendMeConstant.MAPPING_CONTROLLER
			+ SendMeConstant.RECOGNITION_CONTROLLER
			+ SendMeConstant.UPDATE_READ_YN;
//		phoneNumber	
//		readYn	
//		activateYn	
//		modifyDate	
//		sendMeVoiceIdSendMeVoice	
//		sendMeVoiceSender	
		
		SendMeRecognition result = new SendMeRecognition();
		
		try {
			JSONObject parameterJsonObject = new JSONObject();
			parameterJsonObject.put("phoneNumber", sendMeRecognition.getPhoneNumber());
			
			parameterJsonObject.put("readYn", "Y");
			parameterJsonObject.put("activateYn", "Y");
			parameterJsonObject.put("modifyDate", sendMeRecognition.getModifyDate());
			
			parameterJsonObject.put("sendMeVoiceIdSendMeVoice", sendMeRecognition.getSendMeVoiceIdSendMeVoice());
			parameterJsonObject.put("sendMeVoiceSender", sendMeRecognition.getSendMeVoiceSender());
			
			String resultStr = this.requestToServer(url, parameterJsonObject.toString());
			JSONObject resultJsonObject = new JSONObject(resultStr);
			result.setResult(resultJsonObject.getString("result"));		
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	
	
	/**
	 * http protocal 을 사용하는 서버 요청 공통 메소드.
	 * 요청 후 결과를 json string 으로 받아서 반환 한다.
	 * 
	 * @param url
	 * @param inputJsonString
	 * @return
	 */
	public String requestToServer(String url, String inputJsonString) {
		Log.i(TAG, "requestToServer");
		
		String result = "";
		
		try {
			URL postUrl = new URL(url);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/json");
	        OutputStream os = connection.getOutputStream();
	        os.write(inputJsonString.getBytes());
	        os.flush();
	        
	        if(connection != null) {
	        	connection.setConnectTimeout(3000);
	        	
	        	if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        		BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	        		result = bufferedReader.readLine();
	        	}
	        }
		} catch(Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}
}
